// 配置axios
import axios from "axios"
// import { config } from "vue/types/umd"
// 创建axios实例
let httpInstance = axios.create()
// 设置公共的url
// httpInstance.defaults.baseURL = "http://192.168.13.11:3000" || "http://localhost:3000"
httpInstance.defaults.baseURL = "/api"
httpInstance.defaults.timeout = 1000 * 30
// 设置formurl
httpInstance.formurl = (url, data, config) => {
    return httpInstance.post(url, data, {
        headers: {
            "Content-Type": "application/x-www-from-urlencoded",
            // "Content-Type": "multipart/form-data"
        }
    })
}
// request拦截器
httpInstance.interceptors.request.use(
    config => {
        // 解决get无法携带请求头headers
        if (config.method == "get") {
            // 给data赋值以绕过if判断
            config.data = true
        }
        // 设置公共的请求头,需要设置请求头携带token到后端
        config.headers.Authorization = sessionStorage.getItem("token") ? sessionStorage.getItem("token") : ""
        return config

    },
    error => {
        return Promise.reject(error)
    }
)
// response拦截器
// httpInstance.interceptors.response.use(
//     response => {
//         // console.log(response);
//         if (response.status === 200) {
//             // 校验token失效
//             if (response?.data?.code === 202) {
//                 sessionStorage.clear()
//                 return Promise.reject({ code: 202, msg: "token失效,重新登陆" })
//             }
//             return Promise.resolve(response)
//         }

//     },
//     error => {
//         return Promise.reject(error)
//     }
// )

export default httpInstance